package com.ehotting.edsta.mqbd.monitor.task.active.module;

import com.ehotting.eaf.core.utils.NumberHelper;
import com.ehotting.edsta.mqbd.monitor.task.util.TelnetChecker;
import com.ehotting.mqbd.api.bean.solver.SolverInfoBean;
import com.ehotting.mqbd.core.monitor.ModuleActiveAble;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

public abstract class AbstractModuleActive  implements ModuleActiveAble {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public Integer doTelnet(SolverInfoBean solverInfo) {

        MDC.put(ModuleActiveAble.LOGGER_IP_KEY,solverInfo.getIp());
        MDC.put(ModuleActiveAble.LOGGER_PORT_KEY,solverInfo.getPort());

        if(!NumberHelper.isDigital(solverInfo.getPort())){
            logger.info("solverInfo.getPort():{} is wrong format.",solverInfo.getPort());
            return TELNET_FAIL_MASK;
        }

        String host = solverInfo.getIp();
        Integer port = NumberHelper.parseInteger(solverInfo.getPort());

         boolean isConnection = TelnetChecker.checkService(host,port)==true;
         getModuleActiveLogger().info("TelnetChecker[module:{},ip:{},:port:{}] :{}.",this.getModule(),host,port,isConnection?"success":"fail");

         return isConnection ? TELNET_SUCCESS_MASK : TELNET_FAIL_MASK;
    }
}
